"""
Case Type   : 资源池化
Case Name   : 缩容工具查看参数信息、版本信息
Create At   : 2024.10
Owner       : chen
Description :
    1.执行查看参数信息命令
    gs_dropnode --help
    gs_dropnode -?
    2.执行查看版本信息
    gs_dropnode -V
    gs_dropnode --version
Expect      :
    1、显示正常
    2、显示正常
"""

import os
import unittest
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

class SharedStorage(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_node = Node('PrimaryDbUser')
        self.pri_root_node = Node('PrimaryRoot')
        self.db_host = Node('Standby2DbUser').db_host
        self.s_user = self.pri_node.ssh_user
        

    def test_shared_storage(self):
        text = '----- 扩容备二节点 expect: 显示正常-----'
        self.log.info(text)
        xml_path = macro.DB_XML_PATH
        cmd = f'''gs_expansion -U {self.s_user} -G {self.s_user} -X {xml_path} -h {self.db_host}'''
        execute_cmd = f'''source {macro.DB_ENV_PATH};
            expect <<EOF
            set timeout 300
            spawn {cmd}
            expect "*assword*:"
            send "{self.pri_root_node.ssh_password}\\n"
            expect "*assword*:"
            send "{self.pri_root_node.ssh_password}\\n"
            expect "*lease repeat for database*:"
            send "{macro.GAUSSDB_INIT_USER_PASSWD}\\n"
            expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd)
        res = self.pri_root_node.sh(execute_cmd).result()
        self.log.info(res)
        self.assertIn(f'{self.db_host}:\tSuccess', res)

        text = '-----step1: gs_dropnode --help' \
            ' expect: 显示正常-----'
        self.log.info(text)
        cmd = f'''source {macro.DB_ENV_PATH}; gs_dropnode --help'''
        self.log.info(cmd)
        res = self.pri_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn('gs_dropnode is a utility to delete the standby node from a cluster',
        res, text + '失败')

        text = '-----gs_dropnode -? expect: 显示正常-----'
        self.log.info(text)
        cmd = f'''source {macro.DB_ENV_PATH}; gs_dropnode -?'''
        self.log.info(cmd)
        res = self.pri_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn('gs_dropnode is a utility to delete the standby node from a cluster',
        res, text + '失败')

        text = '-----step2: gs_dropnode -V expect: 显示正常-----'
        self.log.info(text)
        cmd = f'''source {macro.DB_ENV_PATH}; gs_dropnode -V'''
        self.log.info(cmd)
        res = self.pri_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn('gs_dropnode', res, text + '失败')

        text = '-----gs_dropnode --version expect: 显示正常-----'
        self.log.info(text)
        cmd = f'''source {macro.DB_ENV_PATH}; gs_dropnode --version'''
        self.log.info(cmd)
        res = self.pri_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn('gs_dropnode', res, text + '失败')

    def tearDown(self):
        self.log.info(f'-----{os.path.basename(__file__)} end-----')